package edu.mit.compilers.assembly.x64.codes;

import edu.mit.compilers.assembly.x64.basic.X64Operand;

public class X64SetCode extends X64Code {

    public enum Type {
        EQ,
        NE,
        LE,
        LT,
        GE,
        GT
    }

    private final X64Operand register;
    private final Type type;

    public X64SetCode(X64Operand register, Type type) {
        this.type = type;
        this.register = register;
        assert register.isRegisterOperand();
        assert register.getUsedRegisters().get(0).bytes == 1;
    }

    @Override
    protected String getStringFroShow(String prefix, boolean debug) {
        return prefix + typeToInstruction(type) + TAB + register.getAddress() + '\n';
    }

    private String typeToInstruction(Type type) {
        switch (type) {
            case EQ:    return "sete";
            case NE:    return "setne";
            case LE:    return "setle";
            case LT:    return "setl";
            case GE:    return "setge";
            case GT:    return "setg";
            default:
                assert false;
                return null;
        }
    }
}
